from pathlib import Path

import casbin
from casbin_pymongo_adapter import Adapter

from conf.config import conf_global


def get_enforcer():
    ip = conf_global.get('mongo', 'ip')
    port = conf_global.getint('mongo', 'port')
    db = conf_global.get('mongo', 'db')
    username = conf_global.get('mongo', 'username')
    password = conf_global.get('mongo', 'password')

    adapter = Adapter(f"mongodb://{username}:{password}@{ip}:{port}/{db}", db)
    parent_dir = Path(__file__).parent
    # print(parent_dir)
    return casbin.Enforcer(str(parent_dir / 'rbac_model.conf'), adapter)


enforcer = get_enforcer()


if __name__ == '__main__':
    # print(conf_global.get('mongo', 'db'))
    get_enforcer()
